package com.situ.stmall.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.situ.stmall.interceptor.CleanOrderInterceptor;
import com.situ.stmall.interceptor.UserInterceptor;

// 相当于SpringMVC的配置文件

@Configuration
public class WebConfig implements WebMvcConfigurer {
	
	@Value("${spring.servlet.multipart.location}")
	private String path;
	
	@Autowired
	private UserInterceptor userInterceptor;
	
	@Autowired
	private CleanOrderInterceptor cleanOrderInterceptor;
	
	// 静态资源映射路径
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/upload/**")
			.addResourceLocations("file:"+path);
	}

	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		// 配置无业务逻辑的路径与视图的映射
		registry.addViewController("/").setViewName("index");

	}
	
	// 添加拦截器
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(userInterceptor)
			.addPathPatterns("/user/**")	// 拦截路径
			.excludePathPatterns("/user/login", "/user/reg");	// 放行路径
		
		registry.addInterceptor(cleanOrderInterceptor)
			.addPathPatterns("/goods", 
					"/user/cart/add", 
					"/api/cart/**",
					"/user/order/add",
					"/api/order/getList");
	}
}




